<template>
  <div class="student-container">
    <xl-search-form :formItems="formItems" @search="search"></xl-search-form>
    <xl-table :columns="columns" :action="loadData" ref="xlTableRef">
      <template slot="avatar" slot-scope="{ row }">
        <img :src="row.user.avatar" />
      </template>
      <template slot="studentNumber" slot-scope="{ row }">
        <span>{{ row.user.username }}</span>
      </template>
      <template slot="birth" slot-scope="{ row }">
        <span>{{ parseTime(row.birth) }}</span>
      </template>
      <template slot="action" slot-scope="{ row }">
        <el-button type="text" @click="handleEdit(row)">编辑</el-button>
      </template>
    </xl-table>

    <el-dialog title="更新学生信息" :visible.sync="dialogVisible" width="50%" @close="resetForm('formRef')">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="姓名" prop="name" style="width: 320px">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker v-model="form.birth" type="date" placeholder="选择日期"> </el-date-picker>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="学院" prop="college" style="width: 320px">
          <el-input v-model="form.college"></el-input>
        </el-form-item>
        <el-form-item label="专业" prop="major" style="width: 320px">
          <el-input v-model="form.major"></el-input>
        </el-form-item>
        <el-form-item label="在校经历">
          <el-input v-model="form.experience" type="textarea" :autosize="{ minRows: 3, maxRows: 6 }"></el-input>
        </el-form-item>
        <el-form-item label="专业技能">
          <el-input v-model="form.skill" type="textarea" :autosize="{ minRows: 3, maxRows: 6 }"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { columns, formItems } from './const'
import { getStudentList, updateStudent } from '@/api/student'
import { parseTime } from '@/utils/index'

export default {
  data() {
    return {
      columns,
      formItems,
      dialogVisible: false,
      form: {},
      rules: {
        name: { required: true, trigger: 'blur', message: '名称不能为空' },
        birth: { required: true, trigger: 'blur', message: '出生年月不能为空' },
        sex: { required: true, trigger: 'blur', message: '性别不能为空' },
        college: { required: true, trigger: 'blur', message: '学院不能为空' },
        major: { required: true, trigger: 'blur', message: '专业不能为空' }
      }
    }
  },
  methods: {
    handleEdit(row) {
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleSubmit() {
      this.$refs['formRef'].validate(async (valid) => {
        if (!valid) return false

        const result = await updateStudent(this.form)
        console.log(result)
        if (result.code === 200) {
          this.$message.success('更新成功')
          this.dialogVisible = false
          this.$refs['xlTableRef'].loadData()
        }
      })
    },
    // 重置
    resetForm(name) {
      if (name) {
        this.$refs[name].resetFields()
      }
      this.dialogVisible = false
    },
    // 格式化时间
    parseTime(value) {
      return parseTime(new Date(value), '{y}-{m}-{d}')
    },
    search(params) {
      this.$refs['xlTableRef'].loadData(params)
    },
    loadData(params) {
      return getStudentList(params)
    }
  }
}
</script>

<style lang="scss" scoped>
img {
  width: 60px;
  height: 60px;
}
</style>
